import time

from appium import webdriver
from appium.options.common import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy as By
from time import sleep

desired_caps = {
    "platformName": "Android",
    "deviceName": "emulator-5554",
    "appPackage": "com.mihoyo.hyperion",
    "appActivity": "com.mihoyo.hyperion.main.HyperionMainActivity"
}
# app的初始化,定义capability,传入四个参数
# desired_caps = {}
# desired_caps["platformName"] = "Android"
# desired_caps["deviceName"] = "emulator-5554"
# desired_caps["appPackage"] = "com.mihoyo.hyperion"
# desired_caps["appActivity"] = "com.mihoyo.hyperion.main.HyperionMainActivity"

# Remote 中的参数：Remote -> appium-server 所在的IP和端口号
# 问题：假如我们把appium-server部署在了非本地，例如appium-server在测试服务器上运行，此时url -> 测试服务器的IP:4723/wd/hub


desired_caps = AppiumOptions()
desired_caps.set_capability("platformName", "Android")
desired_caps.set_capability("deviceName", "emulator-5554")
desired_caps.set_capability("appPackage", "com.mihoyo.hyperion")
desired_caps.set_capability("appActivity", "com.mihoyo.hyperion.main.HyperionMainActivity")
desired_caps.set_capability("noReset", True)  # 是否清除登录状态-不清除
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options=desired_caps)
# 设置隐式等待时间
driver.implicitly_wait(10)

# account = 19121155798
# password = qq081030

# 点击推荐
driver.find_element(By.ID, "com.mihoyo.hyperion:id/mInitChannelBtn").click()
# 点击tab我知道了
driver.find_element(By.ID, "com.mihoyo.hyperion:id/tv_dialog_i_know").click()
# 点击主页我的
driver.find_elements(By.ID, "com.mihoyo.hyperion:id/mHomeTabRadioBtnIv")[3].click()
time.sleep(1)
# 点击登录
# driver.find_element(By.ID, "com.mihoyo.hyperion:id/nickNameTextView").click()
# time.sleep(1)
# 点击账号密码登录
driver.find_element(By.ID, "com.mihoyo.hyperion:id/phone_login_tv_account_tab").click()
time.sleep(1)
driver.find_element(By.ID, "com.mihoyo.hyperion:id/account_login_edit_account").send_keys("19121155798")
driver.find_element(By.ID, "com.mihoyo.hyperion:id/account_login_edit_password").send_keys("qq081030")
# 点击我已阅读并同意
driver.find_element(By.ID, "com.mihoyo.hyperion:id/account_login_iv_agree_check").click()
# 点击登录
driver.find_element(By.ID, "com.mihoyo.hyperion:id/account_login_btn_login").click()
sleep(5)

# 模拟手势操作横向滑动，从x=33 滑动到 x=701 (固定y轴不变，只改变x，就能实现左右滑动)
# TouchAction(driver).press(x=33, y=607).move_to(x=701, y=607).release().perform()
# 模拟手势操作纵向滑动，滑动3次
# for i in range(3):
#     TouchAction(driver).press(x=342, y=1046).wait(1000).move_to(x=342, y=234).release().perform()
#     sleep(2)
# for i in range(3):
#     driver.swipe(start_x=342,start_y=1046,end_x=342, end_y=234,duration=2000)
# sleep(5)
# 练习：在雪球首页模拟向上滑动，连续滑动5次

# driver关闭
# driver.quit()
